import { things } from "./App";
import { deleteAllData,savePlayer } from "./SQL";
/**
 * 通过物品id查找物品
 * @param id 物品id
 * @returns 物品信息
 */
export function find_things_informations_by_id(id: number){
    return things[id];
}
/**
 * 将物品列表转为显示列表
 * @param bagList 物品列表
 * @returns 显示列表
 */
export function bag_show_list(bagList: { id: number; number: number }[]): string[] {
    const list: string[] = bagList.map(item => {
        const xx = find_things_informations_by_id(item.id);
        if (xx) {
            return xx.name + "*" + item.number;
        } else {
            console.warn(`未找到 id 为 ${item.id} 的物品信息`);
            return `未知物品*${item.number}`; // 或者返回 null/undefined，取决于你的需求
        }
    });
    return list;
}
/**
 * 打开玩家背包
 * @param entity 打开背包实体
 * @returns 
 */
export async function openBag(entity:GamePlayerEntity) {
    var option = ['打开背包','保存数据'];
    if (entity.player.name === '稳稳的小鹿9m2'){
        option.push('删档');
    }
    const xswq=find_things_informations_by_id(entity.zswq).name;//显示武器
    const xx = await entity.player.dialog<GameSelectDialogParams>({
        type: GameDialogType.SELECT,
        title: entity.player.name + ' 的玩家信息',
        content: `你好！${entity.player.name}
你的等级：${entity.level}
你的主手武器：${xswq}
请问你要进行什么操作？`,
        titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
        contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
        options: option,
    });
    if (!xx) return;
    if (xx.index === 0){
        var show_list = bag_show_list(entity.bagList);
        const bag = await entity.player.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: entity.player.name + ' 的背包',
            content: `${entity.player.name} 的背包物品`,
            titleBackgroundColor: new GameRGBAColor(0.93, 0.95, 0.54, 1.0),
            contentBackgroundColor: new GameRGBAColor(0.54, 0.68, 0.95, 1.0),
            options: show_list,
        });
        if (!bag) return;
        if (entity.zswq!=0)
            entity.add({id:entity.zswq,number:1});
        entity.delete({id:entity.bagList[bag.index].id,number:1})
        entity.zswq=entity.bagList[bag.index].id;
        entity.player.directMessage('成功穿戴武器');
        
    }else if (xx.value === '删档'){
        await deleteAllData();
        world.querySelectorAll('player').forEach((e)=>{
            e.player.kick();
        })
    }else if (xx.value === '保存数据'){
        await savePlayer(entity);
        entity.player.directMessage('数据保存成功!');
    }
}
